{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-05-17T07:01:14.775329Z",
     "start_time": "2025-05-17T07:01:14.765850Z"
    }
   },
   "source": [
    ""
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-17T07:01:14.783378Z",
     "start_time": "2025-05-17T07:01:14.779323Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "12773b573601eeac",
   "execution_count": null,
   "outputs": []
  },
  {
   "metadata": {},
   "cell_type": "code",
   "execution_count": null,
   "source": "",
   "id": "2e2b4b2f5c990ef8",
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-17T07:01:16.260595Z",
     "start_time": "2025-05-17T07:01:14.787373Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 生成样本数据\n",
    "X = np.array([1, 2, 3, 4, 5])\n",
    "Y = np.array([5, 4, 3, 2, 1])\n",
    "\n",
    "# 计算协方差（总体协方差，ddof=0）\n",
    "cov_manual = np.mean((X - np.mean(X)) * (Y - np.mean(Y)))  # 手动公式计算\n",
    "cov_numpy = np.cov(X, Y, ddof=0)[0, 1]                     # NumPy直接计算\n",
    "\n",
    "print(\"手动计算协方差:\", cov_manual)  # 输出: -2.0\n",
    "print(\"NumPy协方差:\", cov_numpy)     # 输出: -2.0"
   ],
   "id": "c6cc716ebd97e387",
   "execution_count": 1,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-17T07:01:16.265750Z",
     "start_time": "2025-05-17T07:01:16.262450Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "de0cf82df634ef58",
   "execution_count": 1,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-17T07:01:16.569842Z",
     "start_time": "2025-05-17T07:01:16.268745Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 生成多维数据（3维）\n",
    "data = {\n",
    "    \"X1\": [1, 2, 3, 4, 5],\n",
    "    \"X2\": [5, 4, 3, 2, 1],\n",
    "    \"X3\": [2, 3, 4, 5, 6]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "# 计算协方差矩阵（总体协方差，ddof=0）\n",
    "cov_matrix = df.cov(ddof=0)\n",
    "print(\"协方差矩阵:\\n\", cov_matrix)"
   ],
   "id": "3c17401c21aa65ae",
   "execution_count": 2,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-17T07:01:16.576407Z",
     "start_time": "2025-05-17T07:01:16.572838Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "beeda50b007ecb66",
   "execution_count": 2,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-17T07:01:18.174371Z",
     "start_time": "2025-05-17T07:01:17.764040Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.rcParams.update({\n",
    "    'font.size': 14,          # 全局字体大小\n",
    "    'axes.titlesize': 16,     # 标题字体大小\n",
    "    'axes.labelsize': 14,     # 坐标轴标签字体大小\n",
    "    'xtick.labelsize': 12,    # x轴刻度字体\n",
    "    'ytick.labelsize': 12,    # y轴刻度字体\n",
    "    'legend.fontsize': 20     # 图例字体\n",
    "})\n",
    "# 初始化设置\n",
    "plt.rcParams['font.sans-serif'] = ['KaiTi']  # 中文字体\n",
    "plt.rcParams['mathtext.fontset'] = 'stix'     # 数学字体\n",
    "\n",
    "\n",
    "\n",
    "# 绘制热力图\n",
    "plt.figure(figsize=(6, 4))\n",
    "sns.heatmap(cov_matrix, annot=True, cmap=\"coolwarm\")\n",
    "plt.title(\"协方差矩阵热力图\")\n",
    "plt.show()"
   ],
   "id": "cc3deff697e49071",
   "execution_count": 4,
   "outputs": []
  },
  {
   "metadata": {},
   "cell_type": "code",
   "execution_count": null,
   "source": "",
   "id": "958584ba3e71b5c5",
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
